package com.qipay.api.schedule;

import com.qipay.baas.model.CpWithdrawTradlog;
import com.qipay.baas.model.NotifyLog;
import com.qipay.baas.model.Order;
import com.qipay.cpaccount.CpAccountService;
import com.qipay.mapper.SpChannelRuleMapper;
import com.qipay.notify.NotifyLogService;
import com.qipay.order.config.OrderSettleStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.sp.SpChannelRuleService;
import com.qipay.withdraw.service.WithdrawService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CpJobs {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public final static long ONE_Minute = 60 * 1000;
    public final static long ONE_Second = 1000;

    @Autowired
    private OrderService orderService;
    @Autowired
    private CpAccountService cpAccountService;

    @Autowired
    private NotifyLogService notifyLogService;

    @Autowired
    SpChannelRuleService spChannelRuleService;

    @Autowired
    WithdrawService withdrawService;
    @Value("${schedule.open}")
    protected Boolean scheduleOpen;


    /**金额的跳转*/
//    @Scheduled(cron = "0 0 1 * * *")
//    public void account() {
//        if(scheduleOpen){
//            int size = 10;
//            int page = 1;
//            do {
//                List<Order> orders = orderService.getOrderFinishTimeLargeThenOneDay(size, page);
//                if (orders != null && orders.size() > 0) {
//                    orders.stream().forEach(p -> {
//                        try {
//                            //logger.info("set order done:" + p.getTradeNo());
//                            cpAccountService.setOrderDone(p);
//                            p.setSettleStatus(OrderSettleStateEnum.DONE.getType());
//                            orderService.update(p);
//                        } catch (Exception e) {
//                            logger.error("set order done:" + p.getTradeNo());
//                            logger.error(e.getMessage());
//                            e.printStackTrace();
//                        }
//                    });
//                } else {
//                    break;
//                }
//            }while (true);
//        }
//
//    }
    /**通知*/
    @Scheduled(fixedDelay = ONE_Second * 1)
    public void notifyCp() {
        if(scheduleOpen){
            int size = 20;
            List<NotifyLog> notifyList = notifyLogService.getNotifyList(size);
            if (notifyList != null && notifyList.size() > 0) {
                notifyList.stream().forEach(p -> {
                    try {
                        logger.info("send notify:" + p.getId());
                        notifyLogService.send(p);
                    } catch (Exception ex) {
                        logger.error("send notify:" + p.getId());
                        logger.error(ex.toString());
                    }
                });
            }
        }
    }
    /**检查制成功的订单*/
//    @Scheduled(fixedDelay = ONE_Second * 1)
//    public void zhichenggong() {
//        if(scheduleOpen){
//            Order order = orderService.byState((byte)88);
//            try{
//                if(order!=null)orderService.setOrderSuccess(order.getId(),null);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
//    }
    /**提现扣费*/
//    @Scheduled(fixedDelay = ONE_Second * 2)
//    public void widthDraw() {
//        if(scheduleOpen){
//            List<CpWithdrawTradlog>cpWithdrawTradlogs = withdrawService.list((byte)99);
//            if(cpWithdrawTradlogs!=null&&cpWithdrawTradlogs.size()>0){
//                for(CpWithdrawTradlog cc:cpWithdrawTradlogs){
//                    //扣费
//                    Order order = new Order();
//                    order.setCpId(cc.getCpId());
//                    order.setAmount(cc.getAmount());
//                    order.setState((short)99);
//                    order.setSettleStatus((byte)cc.getType());
//                    cpAccountService.setOrderSuccess(order);
//                    //改状态
//                    cc.setState((byte)0);
//                    withdrawService.updateState(cc.getId());
//                    //
//                }
//            }
//        }
//    }
    /**sp管理的额度*/
//    @Scheduled(cron = "0 0 0 * * *")
//    public void resetSpChannelRule() {
//        if(scheduleOpen){
//            spChannelRuleService.resetCurrentTotalAmount();
//        }
//    }
}
